package net.wanho.systemSettings.dao.impl;

import net.wanho.commons.exception.DaoException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.util.JDBCTemplate;
import net.wanho.customerManage.po.LinkMan;
import net.wanho.systemSettings.dao.PositionDao;
import net.wanho.systemSettings.po.Position;
import net.wanho.systemSettings.query.PositionQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class PositionDaoImpl implements PositionDao {
    private JDBCTemplate jdbcTemplate = BeanFactory.getBean(JDBCTemplate.class) ;
    @Override
    public List<Position> selectByCondPosition(PositionQuery positionQuery) throws DaoException {
        final String SELECT_BASE_EMM_POSITION_SQL = "SELECT \tPOSITION_ID POSITIONID, \n" +
                "\tPOSITION_NAME POSITIONNAME, \n" +
                "\tPOSITION_LEVEL POSITIONLEVEL, \n" +
                "\tCREATE_TIME CREATETIME, \n" +
                "\tUPDATE_TIME UPDATETIME\n" +
                "FROM emm_position WHERE 1=1 " ;
        try {
            Object[] params = ArrayUtils.EMPTY_OBJECT_ARRAY ;
            StringBuffer sb = new StringBuffer(SELECT_BASE_EMM_POSITION_SQL);
            if (StringUtils.isNotEmpty(positionQuery.getPositionname())){
                sb.append(" and POSITION_NAME like ?") ;
                params = ArrayUtils.add(params,"%"+positionQuery.getPositionname()+"%") ;
            }
            return jdbcTemplate.queryForList(sb.toString(), Position.class,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }

    @Override
    public Position selectByIdPosition(Integer positionId) throws DaoException {
        final String SELECT_BY_ID_EMM_POSITION_SQL = "SELECT \tPOSITION_ID POSITIONID, \n" +
                "\tPOSITION_NAME POSITIONNAME, \n" +
                "\tPOSITION_LEVEL POSITIONLEVEL, \n" +
                "\tCREATE_TIME CREATETIME, \n" +
                "\tUPDATE_TIME UPDATETIME\n" +
                "FROM emm_position WHERE POSITION_ID =? " ;
        try {
            return jdbcTemplate.queryForObject(SELECT_BY_ID_EMM_POSITION_SQL,Position.class,positionId);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }
}
